#pragma once

#include <furi.h>
#include <furi_hal.h>
#include <gui/gui.h>
#include <gui/view_dispatcher.h>
#include <gui/scene_manager.h>
#include <notification/notification_messages.h>

#include <gui/modules/submenu.h>
#include <gui/modules/popup.h>
#include <gui/modules/loading.h>
#include <gui/modules/text_input.h>
#include <gui/modules/text_box.h>
#include <gui/modules/widget.h>

#include <input/input.h>

#include <lib/nfc/nfc.h>
#include <nfc/nfc_listener.h>
#include <nfc/nfc_poller.h>
#include <nfc/nfc_device.h>

/* generated by fbt from .png files in images folder */
#include <seos_icons.h>

#include "seos.h"
#include "keys.h"
#include "seos_hci.h"
#include "seos_credential.h"
#include "seos_characteristic.h"
#include "seos_native_peripheral.h"
#include "seos_central.h"
#include "seos_common.h"
#include "seos_reader.h"
#include "seos_emulator.h"
#include "scenes/seos_scene.h"
#include "des_cmac.h"
#include "aes_cmac.h"

#define SEOS_TEXT_STORE_SIZE 128

enum SeosCustomEvent {
    // Reserve first 100 events for button types and indexes, starting from 0
    SeosCustomEventReserved = 100,

    SeosCustomEventViewExit,
    SeosCustomEventTextInputDone,
    // Read card events
    SeosCustomEventReaderError,
    SeosCustomEventReaderSuccess,

    SeosCustomEventHCIInit,
    // Events during emulating or reading
    SeosCustomEventScan,
    SeosCustomEventFound,
    SeosCustomEventEmulate,
    SeosCustomEventADFMatched,
    SeosCustomEventAIDSelected,
    SeosCustomEventConnected,
    SeosCustomEventAuthenticated,
    SeosCustomEventSIORequested,
    SeosCustomEventAdvertising,
};

struct Seos {
    bool is_debug_enabled;
    ViewDispatcher* view_dispatcher;
    Gui* gui;
    NotificationApp* notifications;
    SceneManager* scene_manager;

    char text_store[SEOS_TEXT_STORE_SIZE + 1];
    FuriString* text_box_store;

    // Common Views
    Submenu* submenu;
    Popup* popup;
    Loading* loading;
    TextInput* text_input;
    TextBox* text_box;
    Widget* widget;

    Nfc* nfc;
    NfcListener* listener;
    NfcPoller* poller;
    NfcDevice* nfc_device;

    SeosCredential* credential;

    // NFC
    SeosEmulator* seos_emulator;
    SeosReader* seos_reader;

    // BLE
    bool has_external_ble;
    SeosCharacteristic* seos_characteristic;
    SeosCentral* seos_central;
    FlowMode flow_mode;

    bool keys_loaded;
    uint8_t keys_version;
    Bt* bt;
    FuriHalBleProfileBase* ble_profile;
    SeosNativePeripheral* native_peripheral;
};

typedef enum {
    SeosViewMenu,
    SeosViewPopup,
    SeosViewLoading,
    SeosViewTextInput,
    SeosViewTextBox,
    SeosViewWidget,
} SeosView;

void seos_text_store_set(Seos* seos, const char* text, ...);

void seos_text_store_clear(Seos* seos);

void seos_blink_start(Seos* seos);

void seos_blink_stop(Seos* seos);

void seos_show_loading_popup(void* context, bool show);

bool seos_migrate_keys(Seos* seos);
